<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="Hugo 0.98.0" />

  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="author" content="" />
  <meta property="og:url" content="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%8F%98%E9%87%8F/" />
  <link rel="canonical" href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%8F%98%E9%87%8F/" /><link rel="alternate" type="application/atom+xml" href="/index.xml" title="信息管理实验室">

  <script type="application/ld+json">
  {
      "@context" : "http://schema.org",
      "@type" : "BlogPosting",
      "mainEntityOfPage": {
           "@type": "WebPage",
           "@id": "\/"
      },
      "articleSection" : "posts",
      "name" : "入门：变量",
      "headline" : "入门：变量",
      "description" : "程序设计的一个重要内容，就是合理的储存数据，其中，变量能够帮助我们完成数据的储存，而变量的类型则规定了变量储存内容的解析方式。\n使用变量储存数据 在C语言中，想要使用变量，就必须先声明变量，这一语法的基本形式如下：\ntype var_name;\n对于变量的命名而言，有一些重要的规则需要遵循：\n 变量名需要以英文或下划线开头 不能使用 C语言 中的关键字  学习这些规则的最好办法，就是编写程序，因此，我们编写了一些示范的小程序。\n\/* var.c Show the usage of var BeginnerC *\/ #include \u0026lt;stdio.h\u0026gt;int main() {  \/\/ Define a var named number, int type  int number = 100;  \/\/ Define a var named PI, double type  double PI = 3.14;  \/\/ Define a var named letter, char type  char letter = \u0026#39;A\u0026#39;;  printf(\u0026#34;%d %lf %c\\n\u0026#34;, number, PI, letter);  return 0; } 在这个简单的小程序中，我们声明并定义三个变量，选择不同的数据类型来储存不同的数据，并在最后使用 printf 函数输出了他们。",
      "inLanguage" : "en-US",
      "author" : "",
      "creator" : "",
      "publisher": "",
      "accountablePerson" : "",
      "copyrightHolder" : "",
      "copyrightYear" : "2023",
      "datePublished": "2023-01-30 00:00:00 \u002b0000 UTC",
      "dateModified" : "2023-01-30 00:00:00 \u002b0000 UTC",
      "url" : "\/posts\/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97\/%E5%85%A5%E9%97%A8%E5%8F%98%E9%87%8F\/",
      "keywords" : [ "C语言入门指南","C语言","信息管理实验室", ]
  }
</script>
<title>入门：变量</title>
  <meta property="og:title" content="入门：变量" />
  <meta property="og:type" content="article" />
  <meta property="og:description" content="程序设计的一个重要内容，就是合理的储存数据，其中，变量能够帮助我们完成数据的储存，而变量的类型则规定了变量储存内容的解析方式。
使用变量储存数据 在C语言中，想要使用变量，就必须先声明变量，这一语法的基本形式如下：
type var_name;
对于变量的命名而言，有一些重要的规则需要遵循：
 变量名需要以英文或下划线开头 不能使用 C语言 中的关键字  学习这些规则的最好办法，就是编写程序，因此，我们编写了一些示范的小程序。
/* var.c Show the usage of var BeginnerC */ #include &amp;lt;stdio.h&amp;gt;int main() {  // Define a var named number, int type  int number = 100;  // Define a var named PI, double type  double PI = 3.14;  // Define a var named letter, char type  char letter = &amp;#39;A&amp;#39;;  printf(&amp;#34;%d %lf %c\n&amp;#34;, number, PI, letter);  return 0; } 在这个简单的小程序中，我们声明并定义三个变量，选择不同的数据类型来储存不同的数据，并在最后使用 printf 函数输出了他们。" />
  <meta name="description" content="程序设计的一个重要内容，就是合理的储存数据，其中，变量能够帮助我们完成数据的储存，而变量的类型则规定了变量储存内容的解析方式。
使用变量储存数据 在C语言中，想要使用变量，就必须先声明变量，这一语法的基本形式如下：
type var_name;
对于变量的命名而言，有一些重要的规则需要遵循：
 变量名需要以英文或下划线开头 不能使用 C语言 中的关键字  学习这些规则的最好办法，就是编写程序，因此，我们编写了一些示范的小程序。
/* var.c Show the usage of var BeginnerC */ #include &amp;lt;stdio.h&amp;gt;int main() {  // Define a var named number, int type  int number = 100;  // Define a var named PI, double type  double PI = 3.14;  // Define a var named letter, char type  char letter = &amp;#39;A&amp;#39;;  printf(&amp;#34;%d %lf %c\n&amp;#34;, number, PI, letter);  return 0; } 在这个简单的小程序中，我们声明并定义三个变量，选择不同的数据类型来储存不同的数据，并在最后使用 printf 函数输出了他们。" />
  <meta property="og:locale" content="zh-cn" /><meta property="og:image" content="" />
  

  
    <style>body{font-family:bree serif,sans-serif;-webkit-font-smoothing:antialiased;margin:0 20px}article{max-width:800px;margin-left:auto;margin-right:auto}a{color:#000;text-decoration:none}a:hover{font-weight:600;text-decoration:underline}.post-ads{margin:50px 0}.markdown-body{font-size:18px;max-width:100%}.markdown-body a{text-decoration:underline;text-decoration-color:#000}.markdown-body blockquote{margin:0;padding:0 1em;color:#57606a;border-left:.25em solid #d0d7de}.markdown-body pre{padding:16px;overflow:auto;border-radius:10px}.markdown-body code{padding:.2em .4em;font-size:85%;background-color:#f6f8fa;border-radius:6px}.markdown-body pre>code{padding:0;font-size:100%;background-color:inherit;border:0}.Chinese .markdown-body{line-height:200%}.site-date-catalog{font-size:2rem}.header-title{font-size:2rem;font-weight:700;margin-top:32px;font-family:bungee shade,sans-serif}.header-title a{text-decoration:none}.header-subtitle{color:#666}.header-items{margin:10px 0}.header-item{margin:0 5px}.header-line{width:100%;border-width:2px;border-color:#482936;border-style:solid none none none}.lang-switch{font-weight:600}#posts-list{min-height:600px}.posts-line{font-size:1.2rem;margin:12px 0}.posts-categories{font-size:.8rem;margin:auto;text-align:center}.posts-category{padding:3px 0;border:#000 2px solid;border-radius:5px}.site-footer{margin-top:50px}.site-footer-item{margin-right:12px}.post-content img{max-width:100%;display:block;margin-right:auto;margin-top:12px}.post-header{margin-bottom:50px}.post-title{font-size:2rem;font-weight:600}.post-tags{display:inline-block;font-weight:600;padding:2px 5px;margin-right:6px;margin-top:4px;border:#000 2px solid;border-radius:5px}.post-date{font-weight:800;font-style:italic}.post-author{float:right;font-weight:600}.page-content{min-height:60%}.post-content{margin-bottom:50px}.post-content p{hyphens:auto;line-height:1.8;text-justify:ideographic;margin-bottom:1em}.related-content{border-width:3px;border-style:solid;border-color:#000;padding:0 10px;margin-bottom:50px;margin-top:100px}.related-content li{margin:5px 0}.taxonomy-term{font-size:3rem}.gallery-img{text-align:center}.gallery-img span{text-align:center}.gallery-img-desc{font-size:.8em;font-weight:800}@media screen and (max-width:600px){.header-title,.header-subtitle,.header-items{text-align:center}.posts-line{font-size:16px}.markdown-body{font-size:16px}.post-title{font-size:2rem}.post-content p{letter-spacing:.05em}}@media screen and (max-width:48em){.posts-category{display:none}}</style>
  
  
    <style>.container,.container-fluid{margin-right:auto;margin-left:auto}.container-fluid{padding-right:2rem;padding-left:2rem}.row{box-sizing:border-box;display:-webkit-box;display:-ms-flexbox;display:flex;-webkit-box-flex:0;-ms-flex:0 1 auto;flex:initial;-webkit-box-orient:horizontal;-webkit-box-direction:normal;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:wrap;flex-wrap:wrap;margin-right:-.5rem;margin-left:-.5rem}.row.reverse{-webkit-box-orient:horizontal;-webkit-box-direction:reverse;-ms-flex-direction:row-reverse;flex-direction:row-reverse}.col.reverse{-webkit-box-orient:vertical;-webkit-box-direction:reverse;-ms-flex-direction:column-reverse;flex-direction:column-reverse}.col-xs,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9,.col-xs-offset-0,.col-xs-offset-1,.col-xs-offset-10,.col-xs-offset-11,.col-xs-offset-12,.col-xs-offset-2,.col-xs-offset-3,.col-xs-offset-4,.col-xs-offset-5,.col-xs-offset-6,.col-xs-offset-7,.col-xs-offset-8,.col-xs-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-xs{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-xs-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-xs-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-xs-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-xs-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-xs-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-xs-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-xs-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-xs-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-xs-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-xs-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-xs-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-xs-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-xs-offset-0{margin-left:0}.col-xs-offset-1{margin-left:8.33333333%}.col-xs-offset-2{margin-left:16.66666667%}.col-xs-offset-3{margin-left:25%}.col-xs-offset-4{margin-left:33.33333333%}.col-xs-offset-5{margin-left:41.66666667%}.col-xs-offset-6{margin-left:50%}.col-xs-offset-7{margin-left:58.33333333%}.col-xs-offset-8{margin-left:66.66666667%}.col-xs-offset-9{margin-left:75%}.col-xs-offset-10{margin-left:83.33333333%}.col-xs-offset-11{margin-left:91.66666667%}.start-xs{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-xs{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-xs{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-xs{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-xs{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-xs{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-xs{-ms-flex-pack:distribute;justify-content:space-around}.between-xs{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-xs{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-xs{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}@media only screen and (min-width:48em){.container{width:49rem}.col-sm,.col-sm-1,.col-sm-10,.col-sm-11,.col-sm-12,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,.col-sm-7,.col-sm-8,.col-sm-9,.col-sm-offset-0,.col-sm-offset-1,.col-sm-offset-10,.col-sm-offset-11,.col-sm-offset-12,.col-sm-offset-2,.col-sm-offset-3,.col-sm-offset-4,.col-sm-offset-5,.col-sm-offset-6,.col-sm-offset-7,.col-sm-offset-8,.col-sm-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-sm{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-sm-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-sm-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-sm-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-sm-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-sm-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-sm-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-sm-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-sm-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-sm-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-sm-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-sm-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-sm-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-sm-offset-0{margin-left:0}.col-sm-offset-1{margin-left:8.33333333%}.col-sm-offset-2{margin-left:16.66666667%}.col-sm-offset-3{margin-left:25%}.col-sm-offset-4{margin-left:33.33333333%}.col-sm-offset-5{margin-left:41.66666667%}.col-sm-offset-6{margin-left:50%}.col-sm-offset-7{margin-left:58.33333333%}.col-sm-offset-8{margin-left:66.66666667%}.col-sm-offset-9{margin-left:75%}.col-sm-offset-10{margin-left:83.33333333%}.col-sm-offset-11{margin-left:91.66666667%}.start-sm{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-sm{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-sm{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-sm{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-sm{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-sm{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-sm{-ms-flex-pack:distribute;justify-content:space-around}.between-sm{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-sm{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-sm{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:64em){.container{width:65rem}.col-md,.col-md-1,.col-md-10,.col-md-11,.col-md-12,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-md-offset-0,.col-md-offset-1,.col-md-offset-10,.col-md-offset-11,.col-md-offset-12,.col-md-offset-2,.col-md-offset-3,.col-md-offset-4,.col-md-offset-5,.col-md-offset-6,.col-md-offset-7,.col-md-offset-8,.col-md-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-md{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-md-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-md-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-md-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-md-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-md-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-md-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-md-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-md-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-md-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-md-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-md-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-md-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-md-offset-0{margin-left:0}.col-md-offset-1{margin-left:8.33333333%}.col-md-offset-2{margin-left:16.66666667%}.col-md-offset-3{margin-left:25%}.col-md-offset-4{margin-left:33.33333333%}.col-md-offset-5{margin-left:41.66666667%}.col-md-offset-6{margin-left:50%}.col-md-offset-7{margin-left:58.33333333%}.col-md-offset-8{margin-left:66.66666667%}.col-md-offset-9{margin-left:75%}.col-md-offset-10{margin-left:83.33333333%}.col-md-offset-11{margin-left:91.66666667%}.start-md{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-md{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-md{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-md{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-md{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-md{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-md{-ms-flex-pack:distribute;justify-content:space-around}.between-md{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-md{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-md{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}@media only screen and (min-width:75em){.container{width:76rem}.col-lg,.col-lg-1,.col-lg-10,.col-lg-11,.col-lg-12,.col-lg-2,.col-lg-3,.col-lg-4,.col-lg-5,.col-lg-6,.col-lg-7,.col-lg-8,.col-lg-9,.col-lg-offset-0,.col-lg-offset-1,.col-lg-offset-10,.col-lg-offset-11,.col-lg-offset-12,.col-lg-offset-2,.col-lg-offset-3,.col-lg-offset-4,.col-lg-offset-5,.col-lg-offset-6,.col-lg-offset-7,.col-lg-offset-8,.col-lg-offset-9{box-sizing:border-box;-webkit-box-flex:0;-ms-flex:0 0 auto;flex:none;padding-right:.5rem;padding-left:.5rem}.col-lg{-webkit-box-flex:1;-ms-flex-positive:1;flex-grow:1;-ms-flex-preferred-size:0;flex-basis:0;max-width:100%}.col-lg-1{-ms-flex-preferred-size:8.33333333%;flex-basis:8.33333333%;max-width:8.33333333%}.col-lg-2{-ms-flex-preferred-size:16.66666667%;flex-basis:16.66666667%;max-width:16.66666667%}.col-lg-3{-ms-flex-preferred-size:25%;flex-basis:25%;max-width:25%}.col-lg-4{-ms-flex-preferred-size:33.33333333%;flex-basis:33.33333333%;max-width:33.33333333%}.col-lg-5{-ms-flex-preferred-size:41.66666667%;flex-basis:41.66666667%;max-width:41.66666667%}.col-lg-6{-ms-flex-preferred-size:50%;flex-basis:50%;max-width:50%}.col-lg-7{-ms-flex-preferred-size:58.33333333%;flex-basis:58.33333333%;max-width:58.33333333%}.col-lg-8{-ms-flex-preferred-size:66.66666667%;flex-basis:66.66666667%;max-width:66.66666667%}.col-lg-9{-ms-flex-preferred-size:75%;flex-basis:75%;max-width:75%}.col-lg-10{-ms-flex-preferred-size:83.33333333%;flex-basis:83.33333333%;max-width:83.33333333%}.col-lg-11{-ms-flex-preferred-size:91.66666667%;flex-basis:91.66666667%;max-width:91.66666667%}.col-lg-12{-ms-flex-preferred-size:100%;flex-basis:100%;max-width:100%}.col-lg-offset-0{margin-left:0}.col-lg-offset-1{margin-left:8.33333333%}.col-lg-offset-2{margin-left:16.66666667%}.col-lg-offset-3{margin-left:25%}.col-lg-offset-4{margin-left:33.33333333%}.col-lg-offset-5{margin-left:41.66666667%}.col-lg-offset-6{margin-left:50%}.col-lg-offset-7{margin-left:58.33333333%}.col-lg-offset-8{margin-left:66.66666667%}.col-lg-offset-9{margin-left:75%}.col-lg-offset-10{margin-left:83.33333333%}.col-lg-offset-11{margin-left:91.66666667%}.start-lg{-webkit-box-pack:start;-ms-flex-pack:start;justify-content:flex-start;text-align:start}.center-lg{-webkit-box-pack:center;-ms-flex-pack:center;justify-content:center;text-align:center}.end-lg{-webkit-box-pack:end;-ms-flex-pack:end;justify-content:flex-end;text-align:end}.top-lg{-webkit-box-align:start;-ms-flex-align:start;align-items:flex-start}.middle-lg{-webkit-box-align:center;-ms-flex-align:center;align-items:center}.bottom-lg{-webkit-box-align:end;-ms-flex-align:end;align-items:flex-end}.around-lg{-ms-flex-pack:distribute;justify-content:space-around}.between-lg{-webkit-box-pack:justify;-ms-flex-pack:justify;justify-content:space-between}.first-lg{-webkit-box-ordinal-group:0;-ms-flex-order:-1;order:-1}.last-lg{-webkit-box-ordinal-group:2;-ms-flex-order:1;order:1}}</style>
  

  

  <link href="/index.xml" rel="alternate" type="application/rss+xml"
    title="信息管理实验室">
  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css?family=Bree+Serif|Bungee+Shade" rel="stylesheet">
  
  

  
  
</head>


<body>
  <article class="post " id="article">
    <div class="row">
      <div class="col-xs-12">
        <div class="site-header">
          
<header>
  <div class="header-title">
    <a href="/"
      >信息管理实验室</a
    >
  </div>
  <div class="header-subtitle"></div>
</header>
<div class="row end-md center-xs header-items">
  
  <div class="header-item">
    <a href="/posts/%e4%bd%a0%e5%a5%bd%e6%9c%8b%e5%8f%8b/" target="_blank">About</a>
  </div>
  
  <div class="header-item">
    <a href="/tags/%e6%88%91%e4%bb%ac%e7%9a%84%e4%bc%99%e4%bc%b4/" target="_blank">Friends</a>
  </div>
  
  <div class="header-item">
    <a href="/tags/%e7%a4%be%e5%8c%ba%e4%b9%a6%e7%b1%8d/" target="_blank">Books</a>
  </div>
  
</div>
<div class="row end-xs">
   
</div>
<div class="header-line"></div>

        </div>
        <header class="post-header">
          <h1 class="post-title">入门：变量</h1>
          
          <div class="row post-desc">
            <div class="col-xs-6">
              
              <time class="post-date">
                2023-01-30
              </time>
              
              
              <br>
            </div>
          </div>
          <div class="row end-xs">
            
              <a href="/author/%e6%95%ac%e7%ab%a0/"><strong>&nbsp;@敬章</strong></a>        
            
          </div>
          
        </header>

        <div class="post-content markdown-body">
          
          <p>程序设计的一个重要内容，就是合理的储存数据，其中，变量能够帮助我们完成数据的储存，而变量的类型则规定了变量储存内容的解析方式。</p>
<h2 id="使用变量储存数据">使用变量储存数据</h2>
<p>在C语言中，想要使用变量，就必须先声明变量，这一语法的基本形式如下：</p>
<p><code>type var_name;</code></p>
<p>对于变量的命名而言，有一些重要的规则需要遵循：</p>
<ul>
<li>变量名需要以英文或下划线开头</li>
<li>不能使用 C语言 中的关键字</li>
</ul>
<p>学习这些规则的最好办法，就是编写程序，因此，我们编写了一些示范的小程序。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672802851900.png" alt="1672802851900"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    var.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of var
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Define a var named number, int type
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">int</span> number <span style="color:#f92672">=</span> <span style="color:#ae81ff">100</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Define a var named PI, double type
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">double</span> PI <span style="color:#f92672">=</span> <span style="color:#ae81ff">3.14</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">// Define a var named letter, char type
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">char</span> letter <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;A&#39;</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d %lf %c</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, number, PI, letter);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这个简单的小程序中，我们声明并定义三个变量，选择不同的数据类型来储存不同的数据，并在最后使用 printf 函数输出了他们。</p>
<p>其中涉及到的知识点如下</p>
<ol>
<li>C 语言中的数据类型
C 语言提供了数种内置类型进行数据的储存，但<strong>从大的分类来看，主要是整数、小数、字符</strong></li>
<li>printf 格式输出
printf 函数不是一个单纯输出字符串的函数，它可以根据格式指示符（%d %lf %c 这一类）进行对应的数据输出</li>
</ol>
<p>我们将 C语言 中的内置数据类型罗列如下表</p>
<table>
<thead>
<tr>
<th>数据类型</th>
<th>作用说明</th>
<th>具体类型指示符</th>
</tr>
</thead>
<tbody>
<tr>
<td>字符</td>
<td>用于表示可以阅读的符号（比如字母）</td>
<td>char</td>
</tr>
<tr>
<td>小数</td>
<td>表示小数</td>
<td>double float</td>
</tr>
<tr>
<td>整数</td>
<td>表示整数</td>
<td>int</td>
</tr>
</tbody>
</table>
<p>在当下的程序设计中，我们使用 char double int 三种类型对 字符、小数与整数进行储存。</p>
<p>同时，C语言也提供了一些变量限定符，用于更加规范地使用变量。</p>
<table>
<thead>
<tr>
<th>变量限定符</th>
<th>作用</th>
<th>举例</th>
</tr>
</thead>
<tbody>
<tr>
<td>long</td>
<td>使得变量占用的储存空间尽可能大</td>
<td>long double</td>
</tr>
<tr>
<td>short</td>
<td>使得变量占用的储存空间尽可能小</td>
<td>short int</td>
</tr>
<tr>
<td>unsigned</td>
<td>使得变量按照无符号方式进行解析</td>
<td>unsigned int</td>
</tr>
<tr>
<td>signed</td>
<td>使得变量按照有符号方式进行解析</td>
<td>signed int</td>
</tr>
<tr>
<td>const</td>
<td>使得变量不可以被修订</td>
<td>const double PI = 3.1415926;</td>
</tr>
</tbody>
</table>
<p>当然，在现实情况中的程序设计中，情况往往更加复杂，这种状况下，我们就需要灵活选择（用实践作为支撑）</p>
<p>而对于 printf 函数的格式输出，我们也准备了一张对应的表格（描述常用的格式控制符号）</p>
<table>
<thead>
<tr>
<th>指示符号</th>
<th>具体含义</th>
<th>示例代码</th>
</tr>
</thead>
<tbody>
<tr>
<td>%c</td>
<td>输出字符</td>
<td>printf(&quot;%c&quot;, &lsquo;c&rsquo;);</td>
</tr>
<tr>
<td>%s</td>
<td>输出字符串</td>
<td>printf(&quot;%s&quot;, &ldquo;Hello World&rdquo;);</td>
</tr>
<tr>
<td>%d %ld %u</td>
<td>输出整数（以 10 进制的形式）</td>
<td>printf(&quot;%d&quot;, 123);</td>
</tr>
<tr>
<td>%f %lf</td>
<td>输出小数</td>
<td>printf(&quot;%lf&quot;, 3.14);</td>
</tr>
<tr>
<td>%x %o</td>
<td>输出整数（以 16 进制 或 8 进制的形式）</td>
<td>printf(&quot;%x&quot;, 0x0001);</td>
</tr>
</tbody>
</table>
<p>学习 printf 函数的最好方式，就是动手实践，比起拘泥于一些名词与表格，我们更倾向于鼓励你写出一些验证的小程序进行测试。</p>
<h2 id="对数据进行基本处理">对数据进行基本处理</h2>
<p>程序的本质就是处理数据的工具，在完成数据的储存之后，我们就要开始对我们储存的数据进行处理。</p>
<p>在 C语言 中，这被称为运算。</p>
<p>学习数据处理的最好办法，就是动手实践，因此，我们同样会给出一组示例的程序，作为参考。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672804375050.png" alt="1672804375050"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    calc_circle_area.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Calc the circle area
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> pi <span style="color:#f92672">=</span> <span style="color:#ae81ff">3.14</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">float</span> r <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;The area of circle is :%f</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, pi <span style="color:#f92672">*</span> r <span style="color:#f92672">*</span> r);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>这个程序负责计算圆的面积，可以看出，程序非常顺利地达到了我们的目标。</p>
<p>这种包含多个变量运算的式子，被称为表达式，他们是我们数据处理的基石。</p>
<p>C 语言提供了许多种有用的变量运算，我们也如之前一般，整理如下表</p>
<h3 id="加减乘除">加减乘除</h3>
<table>
<thead>
<tr>
<th>符号</th>
<th>作用</th>
<th>举例</th>
</tr>
</thead>
<tbody>
<tr>
<td>+</td>
<td>使两个变量相加</td>
<td>a + b</td>
</tr>
<tr>
<td>-</td>
<td>使两个变量相减</td>
<td>a - b</td>
</tr>
<tr>
<td>*</td>
<td>使两个变量相乘</td>
<td>a * b</td>
</tr>
<tr>
<td>/</td>
<td>使两个变量相除</td>
<td>a / b</td>
</tr>
</tbody>
</table>
<p>学习一种运算，最好的办法，就是动手实践。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672878861547.png" alt="1672878861547"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    calc_base.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of the calc
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> a <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>, b <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d + %d = %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, a, b, a <span style="color:#f92672">+</span> b);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d - %d = %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, a, b, a <span style="color:#f92672">-</span> b);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d * %d = %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, a, b, a <span style="color:#f92672">*</span> b);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d / %d = %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, a, b, a <span style="color:#f92672">/</span> b);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d / %d = %f</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, a, b, a <span style="color:#f92672">/</span> (<span style="color:#66d9ef">double</span>)b);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>可以发现，程序非常顺利地完成了计算任务，唯一的差别在于最后的除法上。</p>
<p>在这里，我们有必要引入表达式类型与类型转化的概念。</p>
<ul>
<li>表达式的类型
首先，我们明确，<strong>在 C语言表达式 的最后结果上，有且只有一种数据类型</strong>
而当参与运算的变量类型不一致时，就需要涉及到类型转换
<ul>
<li>两种类型转换
类型转换分为自动类型转换与强制类型转换
<ul>
<li>自动类型转换
自动转换用“相对更大的储存空间&quot;保证“不损失信息&quot;，它往往取决于参与运算的数据类型中，哪一者的数据类型“占用空间最多&quot;</li>
<li>强制类型转换
强制类型转换由程序设计者们主动写入，语法为
<code>(type)var</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<p>掌握一项程序设计知识点的最好办法，就是动手实践，在此，我们继续编写有关的案例。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672881314067.png" alt="1672881314067"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    calc_base_second.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of the calc
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#ae81ff">3</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">5</span>);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%f</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#ae81ff">3</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">5</span>);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%f</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#ae81ff">3</span> <span style="color:#f92672">/</span> (<span style="color:#66d9ef">double</span>)<span style="color:#ae81ff">5</span>);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%f</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#ae81ff">3</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">5.0</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>可以发现，同样是 3 与 5 相除，四条式子的运算结果却不能一致，这是因为</p>
<ol>
<li>整数除法的结果往往采用舍尾法，即完全忽略小数部分因此 3 / 5 = 0.6 时，后面的小数部分被完全舍去，只剩下整数部分，也就是 0</li>
<li><strong>printf 函数只负责按照指定的格式输出数据</strong>，对于数据的类型（储存方式、占用空间），printf 函数没有决定权
所以，我们尽管在第二个式子中让 printf 函数以小数形式输出数据，也只能得到 0 的结果</li>
<li>在第三个式子中，我们使用强制类型转换，让 5 的类型直接提升到小数型
所以 0.6 被正确的计算了出来</li>
<li>在第四个式子中，我们将 5 变成小数类型，从而让自动类型转化发生，顺利地实现了 0.6 的计算结果</li>
</ol>
<p>同时，对于字符与整数之间的互相转换，我们也提供一个示例</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672916276948.png" alt="1672916276948"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    char_int.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the relation of the char &amp; int
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d = %c</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#e6db74">&#39;A&#39;</span>, <span style="color:#e6db74">&#39;A&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这个案例之中，我们很明显的发现，<strong>字符就是数字的另一种表现形式</strong></p>
<p>而这种“字符-数字&quot;的对应关系，被称为字符集，C语言一般使用 Ascil 字符集</p>
<h3 id="-取余运算">% 取余运算</h3>
<p><strong>取余运算只针对于整数</strong>，它负责计算一次除法以后的余数</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672881978170.png" alt="1672881978170"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    mod.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of mod
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> a <span style="color:#f92672">=</span> <span style="color:#ae81ff">7</span>, b <span style="color:#f92672">=</span> <span style="color:#ae81ff">3</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, a <span style="color:#f92672">%</span> b);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="逻辑运算">逻辑运算</h3>
<p>在 C语言 中，逻辑运算是一种对于数据的比较，它的运算结果遵循一个法则：<strong>非0为真</strong></p>
<p>我们将所有的逻辑运算整合为一张表格，罗列如下</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
<th>举例</th>
</tr>
</thead>
<tbody>
<tr>
<td>&gt;=</td>
<td>判断前者的数据是否大于等于后者</td>
<td>5 &gt;= 3</td>
</tr>
<tr>
<td>&gt;</td>
<td>判断前者的数据是否大于后者</td>
<td>1 &gt; 2</td>
</tr>
<tr>
<td>&lt;=</td>
<td>判断前者的数据是否小于等于后者</td>
<td>6 &lt;= 9</td>
</tr>
<tr>
<td>&lt;</td>
<td>判断前者的数据是否小于后者</td>
<td>0 &lt; 1</td>
</tr>
<tr>
<td>==</td>
<td>判断两个数据是否相等</td>
<td>1 == 1</td>
</tr>
<tr>
<td>!=</td>
<td>判断两个数据是否不相等</td>
<td>0 != 1</td>
</tr>
<tr>
<td>!</td>
<td>将逻辑运算的结果取反</td>
<td>!(1 != 1)</td>
</tr>
</tbody>
</table>
<p>学习一项程序设计知识的最好办法就是动手实践，因此，我们亦给出与之相关的程序</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672886060145.png" alt="1672886060145"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    logical.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of logical calc
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> result <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    result <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span> <span style="color:#f92672">&gt;=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, result);
</span></span><span style="display:flex;"><span>    result <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, result);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这个案例中，我们展现了其中两种逻辑运算，可以很清晰的发现，逻辑运算的结果就是一个整数，判断为真的时候，为1，判断为假的时候，就是0</p>
<p>我们鼓励你更多的用自己编写程序的方式，验证我们上述的观点。</p>
<p>同时，我们指出，这些最基本的逻辑运算，还可以通过组合的方式，构成更加复杂的逻辑关系式。</p>
<p>我们将这些组合的符号，也罗列如下表</p>
<table>
<thead>
<tr>
<th>符号名称</th>
<th>含义</th>
<th>举例</th>
</tr>
</thead>
<tbody>
<tr>
<td>&amp;&amp;</td>
<td>与</td>
<td>(1 != 2) &amp;&amp; (3 == 4)</td>
</tr>
<tr>
<td>||</td>
<td>或</td>
<td>(1 != 2)|| (3 == 4)</td>
</tr>
</tbody>
</table>
<p>他们的区别，我们也用一个程序展示</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672887790585.png" alt="1672887790585"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    or_and_compare.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the difference of &amp;&amp; and ||
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> result;
</span></span><span style="display:flex;"><span>    result <span style="color:#f92672">=</span> (<span style="color:#ae81ff">1</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">2</span>) <span style="color:#f92672">&amp;&amp;</span> (<span style="color:#ae81ff">3</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">4</span>);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, result);
</span></span><span style="display:flex;"><span>    result <span style="color:#f92672">=</span> (<span style="color:#ae81ff">1</span> <span style="color:#f92672">!=</span> <span style="color:#ae81ff">2</span>) <span style="color:#f92672">||</span> (<span style="color:#ae81ff">3</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">4</span>);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, result);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>如您所见，&amp;&amp;（与运算）需要参与表达式同时为真（不为0)时，才可以判断为真；而||（或运算))则只需要其中一个为真即可。</p>
<p>学习，最重要的还是实践，因此，我们鼓励你写出与之相关的程序，验证自己的想法。</p>
<h3 id="条件表达式">条件表达式</h3>
<p>条件表达式根据逻辑运算的结果决定所执行的代码，如您所见</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672888224856.png" alt="1672888224856"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    condition_statement.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of condition statement
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">?</span> puts(<span style="color:#e6db74">&#34;1 &gt; 0&#34;</span>), puts(<span style="color:#e6db74">&#34;That&#39;s true&#34;</span>) <span style="color:#f92672">:</span> puts(<span style="color:#e6db74">&#34;1 &lt;= 0&#34;</span>), puts(<span style="color:#e6db74">&#34;That&#39;s false&#34;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>可以发现，这个程序既顺利，又不顺利的完成了任务。</p>
<p>顺利的在于，条件表达式正确判断出了 <code>1 &gt; 0</code> 的结果，输出了 1 &gt; 0</p>
<p>而不顺利的在于，后面本应该不被输出的 <code>That's false</code> 也被输出了</p>
<p>这是因为，我们使用了 逗号表达式，它的作用在于分割语句为一个个独立的模块。</p>
<p>而在这里，实际上我们的语句被按照如下的方式进行“理解&quot;</p>
<p><code>1 &gt; 0 ? puts(&quot;1 &gt; 0&quot;), puts(&quot;That's true&quot;) : puts(&quot;1 &lt;= 0&quot;)</code> 和 <code>puts(&quot;That's false&quot;);</code></p>
<p>换而言之，不论如何，后面的 <code>That's false</code> 都要被输出。</p>
<p>解决的办法，在于用括号进行包裹。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672888467294.png" alt="1672888467294"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    condition_statement_final.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of condition statement
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1</span> <span style="color:#f92672">&gt;</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">?</span> puts(<span style="color:#e6db74">&#34;1 &gt; 0&#34;</span>), puts(<span style="color:#e6db74">&#34;That&#39;s true&#34;</span>) <span style="color:#f92672">:</span> (puts(<span style="color:#e6db74">&#34;1 &lt;= 0&#34;</span>), puts(<span style="color:#e6db74">&#34;That&#39;s false&#34;</span>));
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>事实上，这种办法非常容易引发不必要的麻烦，也降低了代码的可读性，因此，我们更推荐你使用 <code>if 语句</code> 进行替代。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span>condition <span style="color:#f92672">?</span> statement_true : statement_false;
</span></span><span style="display:flex;"><span><span style="color:#75715e">// Equal to
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">if</span> (condition)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	statement_true;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	statement_false;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>而 <code>if语句</code> 具有更好的可理解性与可阅读性</p>
<h3 id="位运算">位运算</h3>
<p>在计算机中，一切的数据实际上都是二进制数据，而位运算，就是专注于二进制数字的运算。</p>
<p>我们也将他们罗列如下表</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>含义</th>
<th>举例</th>
</tr>
</thead>
<tbody>
<tr>
<td>&raquo;</td>
<td>右移运算，将二进制位向右移动一位，高位补0</td>
<td>1 &raquo; 2</td>
</tr>
<tr>
<td>&laquo;</td>
<td>左移运算，将二进制位向左移动一位，低位补0</td>
<td>1 &laquo; 2</td>
</tr>
<tr>
<td>&amp;</td>
<td>按二进制比特位进行与运算（非逻辑运算）</td>
<td>8 ^ 9</td>
</tr>
<tr>
<td>|</td>
<td>按二进制比特位进行或运算（非逻辑运算）</td>
<td>8 &amp; 9</td>
</tr>
<tr>
<td>^</td>
<td>按二进制比特位进行异或运算（非逻辑运算）</td>
<td>8 ^ 9</td>
</tr>
</tbody>
</table>
<p>学习一项知识点的最好办法，就是动手实践，因此，我们也将给出程序，进行验证</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672909764779.png" alt="1672909764779"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    bit_calc.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of bit calc
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;string.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    PrintNumber
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        Print the number in binary form
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Argument
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        number
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">            The number want to print
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Return Value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">        No value will return
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">PrintNumber</span>(<span style="color:#66d9ef">int</span> number)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> buffer[<span style="color:#ae81ff">1024</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> bit[<span style="color:#ae81ff">2</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> (<span style="color:#ae81ff">0</span> <span style="color:#f92672">==</span> number)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;0&#34;</span>);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">while</span> (number)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        sprintf(bit, <span style="color:#e6db74">&#34;%d&#34;</span>, number <span style="color:#f92672">%</span> <span style="color:#ae81ff">2</span>);
</span></span><span style="display:flex;"><span>        strcat(buffer, bit);
</span></span><span style="display:flex;"><span>        number <span style="color:#f92672">/=</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> strlen(buffer) <span style="color:#f92672">-</span> <span style="color:#ae81ff">1</span>;i <span style="color:#f92672">&gt;=</span> <span style="color:#ae81ff">0</span>;i<span style="color:#f92672">--</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        printf(<span style="color:#e6db74">&#34;%c&#34;</span>, buffer[i]);
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    puts(<span style="color:#e6db74">&#34;&#34;</span>);
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/* Show the usage of &gt;&gt; &amp; &lt;&lt;  */</span>
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">8</span>);
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">8</span> <span style="color:#f92672">&gt;&gt;</span> <span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">8</span> <span style="color:#f92672">&lt;&lt;</span> <span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/* Show the usage of &amp; and ^ and | */</span>
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">4</span>);
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">4</span> <span style="color:#f92672">&amp;</span> <span style="color:#ae81ff">8</span>);
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">4</span> <span style="color:#f92672">^</span> <span style="color:#ae81ff">8</span>);
</span></span><span style="display:flex;"><span>    PrintNumber(<span style="color:#ae81ff">4</span> <span style="color:#f92672">|</span> <span style="color:#ae81ff">8</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>这个程序涉及到了许多超前的知识点，比如循环语句，比如函数，在这里我们只需要清楚一点</p>
<p>PrintNumber 函数会打印出一个十进制数字的二进制数字形式。</p>
<p>而如我们所见，8 的 二进制形式 是 1000，4 的 二进制形式 是 100</p>
<p>先解析前2条语句。</p>
<ol>
<li>8 &raquo; 1，右移一位，所以变成了 0100，也就是图中输出的 100</li>
<li>8 &laquo; 1，左移一位，所以变成了 10000，也就是图中输出的 10000</li>
</ol>
<p>这就是左移与右移运算的基本理解，实际情况下（比如遇到有符号与无符号数字）时会更加复杂，在这里，我们更鼓励你通过实践去掌握他们。</p>
<p>再看后面的三条语句。</p>
<ol>
<li>
<p>4 &amp; 8
也就是 0100 &amp; 1000，因为与运算的准则就是：同时为1才是1，所以这一运算的结果为 0000，也就是最后输出的二进制 0</p>
</li>
<li>
<p>4 ^ 8
也就是 0100 ^ 1000，因为异或运算的准则是：参与对比的两个比特位不一样时，为1，所以这一运算的结果为 1100</p>
</li>
<li>
<p>4 | 8</p>
<p>也就是 0100 | 1000，因为与运算的准则是：两个位中的一位为1，结果就是1，所以这一运算的结果为 1100</p>
</li>
</ol>
<h3 id="取地址运算">取地址运算</h3>
<p><code>&amp;var</code> 可以提取出一个变量的内存地址，这一点对于指针而言颇为重要。</p>
<h3 id="自增运算与自减运算">自增运算与自减运算</h3>
<p><code>var++</code> 将当前变量的数值加一，<code>var--</code>，将当前变量的数值减一</p>
<p><code>var++</code>与 <code>++var</code>，<code>var--</code>与 <code>--var</code>的区别在于最终表达式的结果（是原变量数值，还是自增/自减后的结果)，这一点，我们鼓励你写出明确的程序于以测试。</p>
<h3 id="运算的优先级问题">运算的优先级问题</h3>
<p>每一种运算都有先后次序之分，比如，1 + 2 * 3 的次序就是先计算 2 * 3，再计算 两者 的和。</p>
<p>我们认为，单纯背诵优先级表没有多大的意义，最好的办法就是：</p>
<ol>
<li>使每个表达式都尽可能简单</li>
<li>对于需要优先运算的表达式，用括号进行包裹</li>
<li>动手实践，自己测试</li>
</ol>
<h2 id="计算表达式的占用空间">计算表达式的占用空间</h2>
<p>很多时候，许多复杂的表达式难以估算其类型的占用空间，对此，C语言提供了 sizeof 运算符帮助我们进行评估。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672912928428.png" alt="1672912928428"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    sizeof_try.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use the sizeof to solve the problem
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;A&#39;</span>;
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#66d9ef">sizeof</span>(<span style="color:#ae81ff">3.1415926</span>));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#66d9ef">sizeof</span>(<span style="color:#ae81ff">3.14</span>));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#66d9ef">sizeof</span>(<span style="color:#e6db74">&#39;A&#39;</span>));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#66d9ef">sizeof</span>(<span style="color:#e6db74">&#39;A&#39;</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;B&#39;</span>));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#66d9ef">sizeof</span>(c));
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#66d9ef">sizeof</span>(<span style="color:#ae81ff">5</span> <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>));
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>可以发现，很多时候，表达式所占用的字节空间，是出乎我们的意料的，因此，实践动手才是解决问题的唯一方案。</p>
<h2 id="register-寄存器变量">register 寄存器变量</h2>
<p>寄存器变量相对于一般的变量而言，具有更高的性能。</p>
<p>因为寄存器变量在寄存器中进行读写，而一般的自动变量在内存中进行读写。</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672915752197.png" alt="1672915752197"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    register.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use the register value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;time.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> start <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>, end <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">register</span> <span style="color:#66d9ef">int</span> count_1 <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> count_2 <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    start <span style="color:#f92672">=</span> clock();
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">1000000</span>;i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>        count_1<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>    end <span style="color:#f92672">=</span> clock();
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%f s</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, (<span style="color:#66d9ef">double</span>)(end <span style="color:#f92672">-</span> start) <span style="color:#f92672">/</span> CLOCKS_PER_SEC);
</span></span><span style="display:flex;"><span>    start <span style="color:#f92672">=</span> clock();
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">int</span> i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;i <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">1000000</span>;i<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>        count_2<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>    end <span style="color:#f92672">=</span> clock();
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%f s</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,(<span style="color:#66d9ef">double</span>)(end <span style="color:#f92672">-</span> start) <span style="color:#f92672">/</span> CLOCKS_PER_SEC);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>值得注意的是，<strong>寄存器变量不能使用取地址运算</strong>。</p>
<h2 id="数据的读取">数据的读取</h2>
<p>在能够设计出合理的数据方案之后，我们也要关注数据的读取。</p>
<p>C语言标准函数库 stdio.h 提供了许多有用的函数，帮助我们读取数据。</p>
<p>学习一门技术的最好方式，就是动手实践，下面，我们集中展示他们的使用</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672924647735.png" alt="1672924647735"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    read.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Read the data to the value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> number <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">double</span> number_2 <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;\0&#39;</span>;
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%d %lf %c&#34;</span>, <span style="color:#f92672">&amp;</span>number, <span style="color:#f92672">&amp;</span>number_2, <span style="color:#f92672">&amp;</span>c);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d %f %c</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, number, number_2, c);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这一案例中，我们使用标准库函数 scanf 函数为我们读入 整数 小数 字符 三种类型的数据</p>
<p>而作为一种格式输入函数，scanf 函数也提供了许多其它的用法</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672925176325.png" alt="1672925176325"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    read_2.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Read the data to the value
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> ip_first <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> ip_second <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> ip_third <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">int</span> ip_fourth <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%d.%d.%d.%d&#34;</span>, <span style="color:#f92672">&amp;</span>ip_first, <span style="color:#f92672">&amp;</span>ip_second, <span style="color:#f92672">&amp;</span>ip_third, <span style="color:#f92672">&amp;</span>ip_fourth);
</span></span><span style="display:flex;"><span>    printf(<span style="color:#e6db74">&#34;%d %d %d %d</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, ip_first, ip_second, ip_third, ip_fourth);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这一案例之中，我们用&rsquo;.&lsquo;与&rsquo;%d&rsquo;格式字符串进行组合，最后让我们能够顺利地实现 IP地址（IPV4）的输入</p>
<p>对于 scanf 函数的输入，我们整理如下表</p>
<table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>%%</td>
<td>百分号</td>
</tr>
<tr>
<td>%lf</td>
<td>小数</td>
</tr>
<tr>
<td>%d %u %i</td>
<td>整数</td>
</tr>
<tr>
<td>%s %c</td>
<td>字符串/字符</td>
</tr>
<tr>
<td>%x %o</td>
<td>十六进制数字或八进制数字</td>
</tr>
</tbody>
</table>
<p>值得注意的是，字符串实际上就是多个相连的字符</p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672925891394.png" alt="1672925891394"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    read_3.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Use the scanf to read the string
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> string[<span style="color:#ae81ff">256</span>] <span style="color:#f92672">=</span> {};
</span></span><span style="display:flex;"><span>    scanf(<span style="color:#e6db74">&#34;%s&#34;</span>, string);
</span></span><span style="display:flex;"><span>    printf(string);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这个程序之中，我们使用了数组来保存字符串，可以发现，这个数组的最大容量是 256 个字符。</p>
<p>在我们使用 %s 读取字符串，可以发现，在这个案例之中，我们并没有使用取地址运算，这是因为，<strong>数组名本身就是数组元素的起始地址</strong></p>
<p><img src="image/%E5%8F%98%E9%87%8F/1672926703948.png" alt="1672926703948"></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/*
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    read_4.c
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    Show the usage of getchar/putchar
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">    BeginnerC
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>()
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">char</span> c <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;\0&#39;</span>;
</span></span><span style="display:flex;"><span>    c <span style="color:#f92672">=</span> getchar();
</span></span><span style="display:flex;"><span>    putchar(c);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这个程序中，我们使用 getchar 函数读取单一的字符，并用 putchar 输出他们</p>

        </div>

        <div class="row middle-xs">
          <div class="col-xs-12">
            
            <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/">
                C语言入门指南
              </a>
            </div>
            
            <div class="post-tags">
              <a href="/tags/c%E8%AF%AD%E8%A8%80/">
                C语言
              </a>
            </div>
            
            <div class="post-tags">
              <a href="/tags/%E4%BF%A1%E6%81%AF%E7%AE%A1%E7%90%86%E5%AE%9E%E9%AA%8C%E5%AE%A4/">
                信息管理实验室
              </a>
            </div>
            
          </div>
        </div>
        
          <div class="row">
            <div class="col-xs-12">
              
            </div>
          </div>

          

<div class="related-content">
  <h3>Related Posts</h3>
  <ul>
    
    <li><a href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8c%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E9%80%BB%E8%BE%91%E4%B8%8E%E5%BE%AA%E7%8E%AF/">入门：C语言中的逻辑与循环</a></li>
    
    <li><a href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%87%BD%E6%95%B0/">入门：函数</a></li>
    
    <li><a href="/posts/c%E8%AF%AD%E8%A8%80%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E5%85%A5%E9%97%A8%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2/">入门：字符与字符串</a></li>
    
  </ul>

</div>


          
          
          <div style="height: 50px;"></div>
          
        

        <div class="site-footer">
  
  
</div>

      </div>
    </div>
  </article>

  

<script>
  
  
    
    
  
</script>

  

</body>

</html>